package com.zzs.covid19_view.controller;

import com.alibaba.fastjson.JSON;
import com.zzs.covid19_view.common.entity.AreaStat;
import com.zzs.covid19_view.common.entity.City;
import com.zzs.covid19_view.common.entity.Province;
import com.zzs.covid19_view.service.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 文件名: ShowController
 * 创建者: zzs
 * 创建时间: 2021/8/12 17:42
 * 描述: 显示数据库数据到页面的controller
 */
@Controller
public class ShowController {

    @Autowired
    private DataService dataService;

    /**
     * 首页
     *
     * @param mv
     * @return
     */
    @RequestMapping("/index")
    public ModelAndView index(ModelAndView mv) {
        mv.setViewName("/index");
        //查询所有省份的数据
        List<AreaStat> provinceList = dataService.selectAllProvinceData();
        //默认取值前10个
        List<AreaStat> provinceList_Only10 = provinceList.subList(0, 10);
        //省份名称list
        List<String> provinceNameList = new ArrayList<>();
        //确诊数量list
        List<Integer> confirmedCountList = new ArrayList<>();
        //治愈数量list
        List<Integer> curedCountList = new ArrayList<>();
        //死亡数量list
        List<Integer> deadCountList = new ArrayList<>();
        //疑似数量list
        List<Integer> suspectedCountList = new ArrayList<>();
        //地图数据list
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (AreaStat areaStat : provinceList_Only10) {
            String provinceName = areaStat.getProvinceShortName();
            int confirmedCount = areaStat.getConfirmedCount();
            provinceNameList.add(provinceName);
            confirmedCountList.add(confirmedCount);
            int curedCount = areaStat.getCuredCount();
            int deadCount = areaStat.getDeadCount();
            int suspectedCount = areaStat.getSuspectedCount();
            curedCountList.add(curedCount);
            deadCountList.add(deadCount);
            suspectedCountList.add(suspectedCount);
        }
        for (AreaStat areaStat : provinceList) {
            String provinceName = areaStat.getProvinceShortName();
            int confirmedCount = areaStat.getConfirmedCount();
            int curedCount = areaStat.getCuredCount();
            int deadCount = areaStat.getDeadCount();
            int suspectedCount = areaStat.getSuspectedCount();
            Map<String, Object> map = new HashMap<>();
            map.put("name", provinceName);
            map.put("value", confirmedCount);
            mapList.add(map);
        }
        mv.addObject("max", provinceList.get(0).getConfirmedCount());
        mv.addObject("provinceList", provinceList);
        Collections.reverse(provinceNameList);
        mv.addObject("provinceNameList", JSON.toJSONString(provinceNameList));
        Collections.reverse(confirmedCountList);
        mv.addObject("confirmedCountList", confirmedCountList);
        Collections.reverse(curedCountList);
        mv.addObject("curedCountList", curedCountList);
        Collections.reverse(deadCountList);
        mv.addObject("deadCountList", deadCountList);
        Collections.reverse(suspectedCountList);
        mv.addObject("suspectedCountList", suspectedCountList);
        //给map赋值
        mv.addObject("mapList", JSON.toJSONString(mapList));
        return mv;
    }

    /**
     * 获取城市数据
     *
     * @param request
     * @return
     */
    @RequestMapping("/cityData")
    @ResponseBody
    public Map<String, Object> getCityData(HttpServletRequest request) {
        String provinceShortName = request.getParameter("provinceShortName");
        Map<String ,Object> map = dataService.getCityData(provinceShortName);
        return map;
    }
}
